﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DNA.Mvc.Community
{
    [Table("community_posts")]
    public class Post
    {
        public Post()
        {
            Approved = new DateTime(1800, 1, 1);
            Deleted = new DateTime(1800, 1, 1);
        }

        [Key]
        public virtual int ID { get; set; }

        [Required]
        public int ForumID { get; set; }

        [Required]
        public virtual int ThreadID { get; set; }

        [MaxLength(255),Required]
        public virtual string Title { get; set; }

        [Column(TypeName="ntext")]
        public virtual string Body { get; set; }

        [MaxLength(50)]
        public virtual string DeleteBy { get; set; }

        [Column(TypeName="ntext")]
        public virtual string DeletedReason { get; set; }

        [MaxLength(50)]
        public virtual string IP { get; set; }

        [Required]
        public virtual bool IsApproved { get; set; }

        [Required]
        public virtual bool IsDeleted { get; set; }

        [Required]
        public virtual bool IsLocked { get; set; }

        [Required]
        public virtual bool IsThread { get; set; }
        
        [Required]
        public virtual bool IsAnswer { get; set; }

        [MaxLength(50)]
        public virtual string Moderator { get; set; }

        public virtual int? ParentID { get; set; }
        
        [Required]
        public virtual int Pos { get; set; }

        public virtual int Ratings { get; set; }
        
        [Required]
        public virtual int Reads { get; set; }

        public virtual DateTime Approved { get; set; }

        public virtual DateTime Deleted { get; set; }

        [Required]
        public virtual DateTime LastModified { get; set; }

        [Required]
        public virtual DateTime Posted { get; set; }

        [ForeignKey("ThreadID")]
        public virtual Thread Thread { get; set; }
        
        [MaxLength(50)]
        public virtual string UserName { get; set; }

        public virtual ICollection<Attachment> Attachments { get; set; }

        [NotMapped]
        public bool HasAttachments
        {
            get
            {
                if (Attachments == null)
                    return false;
                return Attachments.Count > 0;
            }
        }
        
        [NotMapped]
        public string SubscriptionToken
        {
            get
            {
                if (this.Thread == null)
                    return "community-" + this.ForumID.ToString() + "-" + this.ThreadID;
                return Thread.SubscriptionToken; 
            }
        }
    }
}
